package com.lsec.springbootinit.controller;

import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.lsec.springbootinit.config.CacheManagerConfig;
import com.lsec.springbootinit.mapper.UserMapper;
import com.lsec.springbootinit.model.common.CommonResult;
import com.lsec.springbootinit.model.common.PageParam;
import com.lsec.springbootinit.model.common.PageResult;
import com.lsec.springbootinit.model.request.UsersReqDTO;
import com.lsec.springbootinit.model.response.UsersResDTO;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;


/**
 * 缓存控制器
 *
 * @author Cherry
 * @date 2023-03-03 14:42
 */

@RestController
@RequiredArgsConstructor
@RequestMapping("cache")
@Slf4j
@Tag(name = "Cache Control")
@ApiSupport(order = 1, author = "222")
public class CacheController {

    private final UserMapper userMapper;

    @GetMapping("test1")
    @Cacheable(value = "test1", unless = "#result == null")
    public Integer test1(Integer id){
        System.out.println("没有走缓存 id = " + id);
        return id;
    }

    @GetMapping("test2")
    @Cacheable(value = "test2", unless = "#result == null", cacheManager = CacheManagerConfig.MANAGER_REDIS_10_SEC)
    public Integer test2(Integer id){
        System.out.println("没有走缓存 id = " + id);
        return id;
    }

    @GetMapping("test3")
    @Cacheable(value = "test3", unless = "#result == null", cacheManager = CacheManagerConfig.MANAGER_REDIS_10_SEC)
    public UsersResDTO test3(@RequestBody UsersReqDTO usersReqDTO){
        System.out.println("没有走缓存 UsersReqDTO = " + usersReqDTO);
        return new UsersResDTO(11L, "DADA",11);
    }



    @GetMapping("list")
    //@Cacheable(value = "list", unless = "#result == null", cacheManager = CacheManagerConfig.MANAGER_REDIS_10_SEC)
    public CommonResult<PageResult<UsersResDTO>> test4(PageParam pageParam){
        System.out.println("没有走缓存 list ");
        return null;

        // 查询
//        Page<Users> usersPage = new Page<>(pageParam.getPage(), pageParam.getPageSize());
//        userMapper.selectPage(usersPage, null);
//        log.info("22222222222222222222");
//        PageResult<Users> usersPageResult = PageUtils.convertToPageResult(usersPage);
//        PageResult<UsersResDTO> usersResDTOPageResult = ErrorCodeConvert.INSTANCE.convertPage(usersPageResult);
//        return success(usersResDTOPageResult);




        // 封装的查询
//        PageResult<Users> usersPageResult = userMapper.selectPage(pageParam, null);
//        PageResult<UsersResDTO> usersResDTOPageResult1 = ErrorCodeConvert.INSTANCE.convertPage(usersPageResult);
//        return success(usersResDTOPageResult1);
    }


    @DeleteMapping("delete")
    @CacheEvict(value = "list", allEntries = true, cacheManager = CacheManagerConfig.MANAGER_REDIS_10_SEC)
    public Integer test5(@RequestParam Long id){
        return userMapper.deleteById(id);
    }
}
